var LifeCell = function(prow, pcol)
{
	this.row = prow;
	this.col = pcol;
	this.status = Math.floor(Math.random() * 100) % 2;
	this.id = this.row.toString() + "_" + this.col.toString();
	this.nextstatus = 0;

	this.getage = function()
	{
		this.status = this.nextstatus;
	}

	this.setnextstatus = function(pl)
	{
		var aroundcount = 0;
		var upleftcell = pl.getitem(this.row - 1, this.col - 1);
		aroundcount += upleftcell.status;
		var upcell = pl.getitem(this.row - 1, this.col);
		aroundcount += upcell.status;
		var uprightcell = pl.getitem(this.row - 1, this.col + 1);
		aroundcount += uprightcell.status;
		var leftcell = pl.getitem(this.row, this.col - 1);
		aroundcount += leftcell.status;
		var rightcell = pl.getitem(this.row, this.col + 1);
		aroundcount += rightcell.status;
		var downleftcell = pl.getitem(this.row + 1, this.col - 1);
		aroundcount += downleftcell.status;
		var downcell = pl.getitem(this.row + 1, this.col);
		aroundcount += downcell.status;
		var downrightcell = pl.getitem(this.row + 1, this.col + 1);
		aroundcount += downrightcell.status;

		if((aroundcount > 3) || (aroundcount < 2))
		{
			this.nextstatus = 0;
		}
		else
		{
			if(this.status == 0)
			{
				if(aroundcount == 3)
				{
					this.nextstatus = 1;
				}
				else
				{
					this.nextstatus = 0;
				}
			}
			else
			{
				this.nextstatus = 1;
			}
		}
	}
}

var DeadCell = function(prow, pcol)
{
	this.row = prow;
	this.col = pcol;
	this.status = 0;
}

var LifeCellArr = function()
{
	this.items = new Array();
	this.items.length = 0;
	this.add = function(pobj)
	{
		this.items[this.items.length] = pobj;
	}
	this.getitem = function(pr, pc)
	{
		for(var icnt = 0; icnt < this.items.length; icnt++)
		{
			var myi = this.items[icnt];
			var bmatch = true;
			bmatch = bmatch && (myi.row == pr);
			bmatch = bmatch && (myi.col == pc);
			if(bmatch)
			{
				return myi;
			}
		}
		return (new DeadCell(pr, pc));
	}
}

var LifeTable = function(pid)
{
	this.rootid = pid;
	this.rowcount = parseInt(document.getElementById(pid).getAttribute("rows"));
	this.colcount = parseInt(document.getElementById(pid).getAttribute("cols"));
	this.instancename = document.getElementById(pid).getAttribute("instancename");
	this.mycanvasid = this.instancename + "canvassssssssss" + (new Date()).getTime().toString();
	this.mydivid = this.instancename + "divvvvvvv" + (new Date()).getTime().toString();

	this.started = true;

	this.cells = new LifeCellArr();

	this.generatecells = function()
	{
		for(var rcnt = 0; rcnt < this.rowcount; rcnt++)
		{
			for(var ccnt = 0; ccnt < this.colcount; ccnt++)
			{
				this.cells.add(new LifeCell(rcnt, ccnt));
			}
		}
	}
	this.generatecells();
	var myd = document.body.appendChild(document.createElement("canvas"));
	myd.setAttribute("id", this.mycanvasid);
	myd.style.zIndex = 100;
	myd.style.position = "absolute";
	myd.style.left = 0;
	myd.style.top = 0;
	myd.width = window.innerWidth;
	myd.height = window.innerHeight;
	myd.setAttribute("onclick", this.instancename + ".stop();");
	var mycx = myd.getContext("2d");
	mycx.beginPath();
	mycx.fillStyle = "rgba(0,0,0,0.7)";
	mycx.fillRect(0,0,myd.width, myd.height);
	mycx.closePath();

	var mydiv = document.body.appendChild(document.createElement("div"));
	mydiv.setAttribute("id", this.mydivid);
	mydiv.style.zIndex = 101;
	mydiv.style.position = "absolute";
	mydiv.style.left = "200px";
	mydiv.style.top = "200px";

	this.stop = function()
	{
		this.started = false;
	}

	this.display = function()
	{
		if(this.started)
		{
			var roote = document.getElementById(this.mydivid);
			roote.innerHTML = "";
			roote.appendChild(this.generatetable());
			setTimeout(this.instancename + ".getage();", 100);
		}
		else
		{
			var mya = document.getElementById(this.mycanvasid);
			mya.parentNode.removeChild(mya);
			mya = document.getElementById(this.mydivid);
			mya.parentNode.removeChild(mya);
		}
	}

	this.getage = function()
	{
		for(var icnt = 0; icnt < this.cells.items.length; icnt++)
		{
			this.cells.items[icnt].setnextstatus(this.cells);
			this.cells.items[icnt].getage();
		}
		this.display();
	}

	this.generatetable = function()
	{
		var t = document.createElement("table");
		for(var rcnt = 0; rcnt < this.rowcount; rcnt++)
		{
			var r = t.appendChild(document.createElement("tr"));
			for(var ccnt = 0; ccnt < this.colcount; ccnt++)
			{
				var d = r.appendChild(document.createElement("td"));
				var c = d.appendChild(document.createElement("canvas"));
				c.width = 10;
				c.height = 10;
				if(this.cells.getitem(rcnt, ccnt).status == 1)
				{
					var cx = c.getContext("2d");
					cx.beginPath();
					cx.fillStyle = "lightgreen";
					cx.fillRect(0,0,c.width, c.height);
					cx.closePath();
				}
			}
		}
		return t;
	}
	this.display();
}
